package com.yksj.healthtalk.ui.doctorstation;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.recharge.ChoiseDateActivity;
import com.yksj.healthtalk.utils.TimeUtil;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 选择历史          (服务内容中点击的选择历史)
 * @author Administrator
 *
 */
public class DoctorServiceHistoryListActivity extends BaseFragmentActivity implements OnClickListener, OnRefreshListener<ListView> {

	private int loadNum=0;
	private String date="";
	private PullToRefreshListView mPullListView;
	private ListView mListView;
	private TextView mText;
	private Myadapter adapter;
	private String SERVICE_CONTENT_ID;
	private TextView textConut;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctorservice_history_list_activity_layout);
		initView();
	
	}

	private void initView() {
		initTitle();
		titleTextV.setText("服务设置历史");
		titleLeftBtn.setOnClickListener(this);
		findViewById(R.id.calender).setOnClickListener(this);
		mText = (TextView) findViewById(R.id.date_text);
		mText.setOnClickListener(this);
		findViewById(R.id.calender_button).setOnClickListener(this);
		textConut = (TextView) findViewById(R.id.count);
		mPullListView = (PullToRefreshListView)findViewById(R.id.hall);
		mListView = mPullListView.getRefreshableView();
		mPullListView.setOnRefreshListener(this);
		TextView textView=new TextView(this);
		textView.setText("还没有患者哦");
		textView.setGravity(Gravity.CENTER);
		mListView.setEmptyView(textView);
		adapter = new Myadapter(this);
		mListView.setAdapter(adapter);
		SERVICE_CONTENT_ID=getIntent().getStringExtra("SERVICE_CONTENT_ID");
		initData();
	}

	private void initData() {
		HttpRestClient.doHttpDoctorServiceContentHistory(date,SERVICE_CONTENT_ID,new JsonsfHttpResponseHandler(this){
			@Override
			public void onFinish() {
				super.onFinish();
				mPullListView.onRefreshComplete();
			}
			
			@Override
			public void onSuccess(int statusCode,JSONArray response) {
				super.onSuccess(statusCode, response);
				adapter.onChangeDada(response);
				textConut.setText("详细记录共("+response.size()+")条");
			}
			
			@Override
			public void onFailure(Throwable e, JSONArray errorResponse) {
				super.onFailure(e, errorResponse);
				ToastUtil.showBasicErrorShortToast(getApplicationContext());
			}
		});
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		super.onActivityResult(arg0, arg1, data);
		switch (arg0) {
		case 1000:
			if(data!=null){
				Bundle extras = data.getExtras();
				if(extras.containsKey("all")){
					date="";
					mText.setText("全部");
				}else{
					if(extras.containsKey("nothing"))return;
					String string = extras.getString("date");
					String string1 = extras.getString("year");
					mText.setText(string1+"-"+string);
					date=string1+string;
					}
				loadNum=0;
				adapter.mList.clear();
			}else{
					date="";
					mText.setText(TimeUtil.getMoneyTime().substring(0,4)+"");
			}
			initData();
			break;
		}
		
		
	}
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.calender:
		case R.id.date_text:
		case R.id.calender_button:
			intent=new Intent(this,ChoiseDateActivity.class);
			if(mText.getText().toString().length()<5){
				intent.putExtra("date", TimeUtil.getMoneyTime());
			}else{
				intent.putExtra("date", mText.getText().toString());
			}
			startActivityForResult(intent,1000);
			break;
		}
		
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadNum++;
		initData();
	}
	
	
	class Myadapter extends BaseAdapter{

		public JSONArray mList=new JSONArray();
		private LayoutInflater from;
		public Myadapter(Context con) {
			from = LayoutInflater.from(con);
		}

		public void onChangeDada(JSONArray array){
			mList=array;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public JSONObject getItem(int position) {
			return mList.getJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			JSONObject jsonObj = getItem(position);
			if(convertView==null){
				holder=new ViewHolder();
				convertView=from.inflate(R.layout.doctor_service_content_item_layout, null);
				holder.name=(TextView) convertView.findViewById(R.id.name);
				holder.time=(TextView) convertView.findViewById(R.id.time);
				holder.isChoise=(TextView) convertView.findViewById(R.id.ischoise);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			holder.name.setText(jsonObj.getString("SERVICE_CONTENT"));
			holder.time.setText(TimeUtil.format(jsonObj.getString("SELECT_CANCEL_TIME")));
			holder.isChoise.setText(jsonObj.getString("SERVICE_TYPE").equals("1")?"已选择":"已取消");
			return convertView;
		}
		
		class ViewHolder{
			private TextView name;
			private TextView time;
			private TextView isChoise;
		}
	}
}
